package com.aaa.web.shiro;

import com.aaa.entity.Employee;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.util.DigestUtils;

import java.util.UUID;

/**
 * shiro框架相关
 * @Author: yyh
 * @Date: 2018/12/13 0013 10:27
 * @Version 1.0
 */
public class ShiroUtil {
    public static String md5(String text, String key) throws Exception {
        //加密后的字符串
        String encodeStr=DigestUtils.md5DigestAsHex(text.getBytes());
        return encodeStr;
    }

    /**
     * 对用户的密码进行加盐加密,使用md5方式
     * @param emp
     * @return
     */
    public static Employee encryptPassword(Employee emp){
        //加密的次数
        int hashIterations = 1;
        //盐值
        String salt = emp.getTelephone();
        //密码
        Object credentials = emp.getPassword();
        //加密方式
        String hashAlgorithmName = "MD5";
        String enPassword = new SimpleHash(hashAlgorithmName, credentials,
                salt, hashIterations).toString();
        System.out.println("加密的盐值----->" + salt);
        System.out.println("加密后的值----->" + enPassword);
        emp.setPassword(enPassword);
        return emp;
    }

    public static void main(String[] args) throws Exception {
        //加密的次数
        int hashIterations = 1;
        String salt = "18811112222";
        //密码
        Object credentials = "123456";
        //加密方式
        String hashAlgorithmName = "MD5";
        SimpleHash simpleHash = new SimpleHash(hashAlgorithmName, credentials, salt,
                 hashIterations);
        System.out.println(simpleHash.toString());
    }
    public static String getPassword(String telephone,String password){
        int hashIterations = 1;
        String salt = telephone;
        //密码
        Object credentials = password;
        //加密方式
        String hashAlgorithmName = "MD5";
        SimpleHash simpleHash = new SimpleHash(hashAlgorithmName, credentials, salt,
                hashIterations);
        return simpleHash.toString();
    }
}
